//
//  SWOriginalView.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/23.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


class SWOriginalView: UIView {
    
    
    var originalPictureViewTopConstraint:Constraint?
    

    var originalViewModel:SWOriginalViewModel?{
        
        didSet{
            
            
            //1.头像
           let url = NSURL(string: originalViewModel?.homeModel?.user?.profile_image_url ?? "")    
           userImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            //2.名字
            nameLabel.text = originalViewModel?.homeModel?.user?.name
            //3.内容
            contentLabel.text = originalViewModel?.homeModel?.text
            //4.等级
            memberImageView.image = originalViewModel?.memberImage

            // 5. 发布时间
            
            timeLabel.text = originalViewModel?.timeText
            // 6. 微博来源
            sourceLabel.attributedText = originalViewModel?.sourceText
            
            //7. 原创微博的照片
            //写在照片的约束
            originalPictureViewTopConstraint?.uninstall()
            
            //如果有原创微博的照片
            if originalViewModel?.homeModel?.pic_urls?.count > 0 {
 
                pictureView.dataArray = originalViewModel?.homeModel?.pic_urls
                
                pictureView.hidden = false
                
                pictureView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.originalPictureViewTopConstraint = make.top.equalTo(contentLabel.snp_bottom).offset(10).constraint
                    
                    
                })
                
                
                
            }else{
                
                pictureView.hidden = true
                
            }
    
        }
 
    }
    
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件
    private func setupUI(){
        
        
        self.backgroundColor = UIColor.whiteColor()
        
        
        addSubview(userImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)

        
        //1.头像
        userImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(10)
            make.size.equalTo(CGSizeMake(50, 50))
        }
        //2.名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(userImageView.snp_trailing).offset(10)
            make.top.equalTo(userImageView)
        }
        //测试数据
        nameLabel.text = "用户名"
        
        //3.等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        //4.时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(userImageView.snp_trailing).offset(10)
            make.bottom.equalTo(userImageView)
        }
        //测试数据
  
        
        //5.来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        //测试数据

        
        //6.原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(10)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
   
         //   make.bottom.equalTo(self).offset(-10)
 
        }
        

        //7.原创微博配图
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
           originalPictureViewTopConstraint = make.top.equalTo(contentLabel.snp_bottom).offset(10).constraint
         
            make.leading.equalTo(self).offset((ScreenWidth - (3 * ItemWH + 2 * ItemMargin))/2)
            
            //相对于父视图的底部约束拿到外面交给父视图设置
         //   make.bottom.equalTo(self).offset(-10)
        }
        
        
        
        
    }
    
    //懒加载控件
    //1.头像
    private lazy var userImageView:UIImageView  = {
        
        let iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconImageView.cornerRadiu = 25
        
        iconImageView.layer.masksToBounds = true
        
        return iconImageView
    }()
    //2.名字
    private lazy var nameLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //3.等级
    private lazy var memberImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vgirl"))
    //4.时间
    private lazy var timeLabel:UILabel = UILabel(textColor: THEMECOLOR, fontSize: 15)
    //5.来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: THEMECOLOR, fontSize: 15)
    //6.原创微博内容
    lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, preferredMaxLayoutWidth: ScreenWidth-20)
    
    //7.原创微博配图
    
    lazy var pictureView:SWPictureCollectionView = SWPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    

}